package com.jianglin.mapper;

import com.jianglin.model.Userinfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component("userinfoDao")
public interface UserinfoMapper{

    @Select("<script>" +
            "SELECT * FROM userinfo " +
            "<where>" +
            "   <if test='type != null'> AND type = #{type} </if>" +
            "</where>" +
            "</script>")
    List<Userinfo> queryUserinfoAll(Userinfo userinfo);

    @Select("SELECT * FROM userinfo WHERE username = #{username} AND password = #{password} AND type = #{type}")
    Userinfo queryUserByNameAndPwd(Userinfo userinfo);

    @Delete("DELETE FROM userinfo WHERE username = #{username2}")
    void deleteUserByUsername(@Param("username2") String username);

    // 新增基础CRUD方法（替代BaseMapper功能）
    @Select("SELECT * FROM userinfo WHERE id = #{id}")
    Userinfo selectById(Long id);

    @Insert("INSERT INTO userinfo (username, password, type, remarks) " +
            "VALUES (#{username}, #{password}, #{type}, #{remarks})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Userinfo userinfo);

    @Update("<script>" +
            "UPDATE userinfo " +
            "<set>" +
            "   <if test='username != null'>username = #{username},</if>" +
            "   <if test='password != null'>password = #{password},</if>" +
            "   <if test='type != null'>type = #{type},</if>" +
            "   <if test='remarks != null'>remarks = #{remarks},</if>" +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    int update(Userinfo userinfo);

    @Delete("DELETE FROM userinfo WHERE id = #{id}")
    int deleteById(Long id);

    // UserinfoMapper.java 添加以下方法
    @Select("SELECT * FROM userinfo WHERE username = #{username}")
    Userinfo findUserByName(String username);
}
